package com.lb.shopguide.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lb.shopguide.R;

/**
 * Created by kang on BOTTOM_MIJIBOTTOM_HOME17/5/BOTTOM_MINEBOTTOM_HOME.
 */

public class LbBottomBarBoss extends LinearLayout implements View.OnClickListener {

    public static final int BOTTOM_MERCHANT = 0;
    public static final int BOTTOM_SECRETARY = 1;
    public static final int BOTTOM_MINE = 2;

    private LinearLayout layoutMerchant;
    private LinearLayout layoutSecretary;
    private LinearLayout layoutMine;

    private ImageView ivMerchant;
    private ImageView ivSecretary;
    private ImageView ivMine;

    private TextView tvMerchant;
    private TextView tvSecretary;
    private TextView tvMine;

    private int pos;
    private int colorSelected = Color.rgb(247,85,78);
    private int colorUnSelected = Color.rgb(172, 172, 172);

    public LbBottomBarBoss(Context context) {
        super(context);
    }

    public LbBottomBarBoss(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_lb_bottombar_boss, this);
        tvMerchant = findViewById(R.id.tv_merchant);
        tvSecretary = findViewById(R.id.tv_secretary);
        tvMine = findViewById(R.id.tv_mine);

        ivMerchant = findViewById(R.id.iv_merchant);
        ivSecretary = findViewById(R.id.iv_secretary);
        ivMine = findViewById(R.id.iv_mine);

        layoutMerchant = findViewById(R.id.layout_merchant);
        layoutSecretary = findViewById(R.id.layout_secretary);
        layoutMine = findViewById(R.id.layout_mine);

        layoutMerchant.setOnClickListener(this);
        layoutSecretary.setOnClickListener(this);
        layoutMine.setOnClickListener(this);

        setChecked(BOTTOM_MERCHANT);
    }

    public LbBottomBarBoss(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setChecked(int pos) {
        clearCheck();
        switch (pos) {
            case BOTTOM_MERCHANT:
                ivMerchant.setSelected(true);
                tvMerchant.setTextColor(colorSelected);
                break;
            case BOTTOM_SECRETARY:
                ivSecretary.setSelected(true);
                tvSecretary.setTextColor(colorSelected);
                break;
            case BOTTOM_MINE:
                ivMine.setSelected(true);
                tvMine.setTextColor(colorSelected);
                break;
        }
        this.pos = pos;
    }

    private void clearCheck() {
        ivMerchant.setSelected(false);
        tvMerchant.setTextColor(colorUnSelected);

        ivSecretary.setSelected(false);
        tvSecretary.setTextColor(colorUnSelected);

        ivMine.setSelected(false);
        tvMine.setTextColor(colorUnSelected);
    }

    @Override
    public void onClick(View v) {
        if (mOnTabSelectedListener != null) {
            switch (v.getId()) {
                case R.id.layout_merchant:
                    if (pos != BOTTOM_MERCHANT) {
                        mOnTabSelectedListener.onTabSelected(BOTTOM_MERCHANT, pos);
                        setChecked(BOTTOM_MERCHANT);
                    }
                    break;
                case R.id.layout_secretary:
                    if (pos != BOTTOM_SECRETARY) {
                        mOnTabSelectedListener.onTabSelected(BOTTOM_SECRETARY, pos);
                        setChecked(BOTTOM_SECRETARY);
                    }
                    break;
                case R.id.layout_mine:
                    if (pos != BOTTOM_MINE) {
                        mOnTabSelectedListener.onTabSelected(BOTTOM_MINE, pos);
                        setChecked(BOTTOM_MINE);
                    }
                    break;
            }
        }
    }

    public void performClick(int newpos) {
        mOnTabSelectedListener.onTabSelected(newpos, pos);
        setChecked(newpos);
    }

    public interface OnTabSelectedListener {
        void onTabSelected(int pos, int prePos);
    }

    public void setOnTabSelectedListener(OnTabSelectedListener listener) {
        mOnTabSelectedListener = listener;
    }

    private OnTabSelectedListener mOnTabSelectedListener;
}
